import { EggAppConfig, EggAppInfo, PowerPartial } from 'egg';

export default (appInfo: EggAppInfo) => {
  const config = {} as PowerPartial<EggAppConfig>;

  // override config from framework / plugin
  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_1596598360939_4892';
  config.jwt = {
    secret: 'egg',
  };
  // add your egg config in here
  config.middleware = ['authen'];
  config.authen = ['/api/login', '/swagger-ui.html', '/api/register', '/']
  //数据库配置
  config.mysql = {
    // 单数据库信息配置
    client: {
      // host
      host: 'localhost',
      // 端口号
      port: '3307',
      // 用户名
      user: 'root',
      // 密码
      password: 'a123456',
      // 数据库名
      database: 'test',
    },
    // 是否加载到 app 上，默认开启
    app: true,
    // 是否加载到 agent 上，默认关闭
    agent: false,
  }
  // 配置 swagger
  const useConfig = {
    swaggerdoc: {
      dirScanner: "./app/controller", // 配置自动扫描的控制器路径。
      apiInfo: {
        title: "EGGTS",
        description: "swagger-ui for 来自蛋蛋的api.",
        version: "1.0.0",
      },
      schemes: ["http", "https"],
      consumes: ["application/json"],
      produces: ["application/json"],
      securityDefinitions: {
        // apikey: {type: "apiKey", name: "clientkey", in: "header",},
        // oauth2: {
        //     type: "oauth2", tokenUrl: "http://petstore.swagger.io/oauth/dialog", flow: "password",
        //     scopes: {
        //         "write:access_token": "write access_token",
        //         "read:access_token": "read access_token",
        //     },
        // },
      },
      enableSecurity: false, // 是否启用授权，默认 false（不启用）。
      // enableValidate: true,    // 是否启用参数校验，默认 true（启用）。
      routerMap: true, // 是否启用自动生成路由，默认 true (启用)。
      enable: true, // 默认 true (启用)。
    },
  } as const;
  // 安全配置
  config.security = {
    csrf: {
      enable: false,
      ignoreJSON: true,
    },
    domainWhiteList: ['*'],
  };
  // 跨域配置
  config.cors = {
    origin: '*',
    allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH',
  };
  config.validate = {
    convert: false,
    widelyUndefined: true,
  };
  // add your special config in here
  const bizConfig = {
    sourceUrl: `https://github.com/eggjs/examples/tree/master/${appInfo.name}`,
  };

  // the return config will combines to EggAppConfig
  return {
    ...config,
    ...bizConfig,
    ...useConfig,
  };
};
